from django import forms

from App.bootstrap import BootStrapModelForm
from App.models import Company


class CompanyForm(BootStrapModelForm):
    name_company = forms.CharField(
        label='公司名',
        widget=forms.TextInput(attrs={
            'placeholder': '请输入公司名',
        })
    )

    user_company = forms.CharField(
        label='公司实际管理员',
        widget=forms.TextInput(attrs={
            'placeholder': '请输入管理员姓名',
        })
    )

    info_company = forms.CharField(
        label='公司简介',
        widget=forms.TextInput(attrs={
            'placeholder': '请输入公司简介',
        })
    )

    img_index = forms.ImageField(
        label='主页宣传图',
    )

    img_company = forms.ImageField(
        label='详情图',
    )

    phone_company = forms.CharField(
        min_length=11,
        max_length=11,
        label='联系电话',
        widget=forms.TextInput(attrs={
            'placeholder': '请输入联系电话',
        })
    )

    class Meta:
        model = Company
        fields = '__all__'

    # 判断视频格式
    def clean_video_company(self):
        file_object = self.cleaned_data.get('video_company')
        if file_object.name.split('.')[-1] not in ['avi', 'wmv', 'mpeg', 'mp4', 'mov', 'rm', 'ram']:
            raise forms.ValidationError('文件格式错误')
        else:
            return file_object